<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {
	
	protected $_data = array();
	protected $is_login;
	protected $loginer;
	protected $p;
	
	protected  $tbl_reply = 'reply';
	protected $tbl_topic = 'topic';
	protected $tbl_img = 'img';
	protected $tbl_follow = 'follow';
	protected $tbl_dig = 'dig';
	protected $tbl_favo = 'favo';
	protected $tbl;
	
	protected $list_cat;
	
    function __construct(){
        parent::__construct();
    	date_default_timezone_set('PRC');
    	
    	$is_first_access = $this->session->userdata('is_first_access');
    	$this->_data['is_first_access'] = $is_first_access;
    	if(empty($is_first_access)){
       	    $this->session->set_userdata('is_first_access',mktime());
    	}

    	//取得所有表
    	$this->tbl = (object)config_item('tbl');
    	
    	//加载通用模型
    	$this->load->model('common_model','cm');
    	
        //用户是否登录
        $loginer = $this->session->userdata('loginer');
        $is_login = !empty($loginer->id) ? true : false;
        $this->_data['is_login'] = $is_login;
        $this->is_login = $is_login; 
        
        //登录者信息
        if($is_login){
        	$this->_data['loginer'] = $loginer; 
        	$this->loginer = $loginer;
    	    //未读私信数
    	    $this->_data['count_no_r'] = $this->cm->count($this->tbl->msg,array('to_id'=>$this->loginer->id,'is_r'=>0));

    	    /*
        }else{
            if(
                $_REQUEST['c'] == 'member' && $_REQUEST['m'] == 'login' ||
                $_REQUEST['c'] == 'member' && $_REQUEST['m'] == 'ajax_login' ||
                $_REQUEST['c'] == 'member' && $_REQUEST['m'] == 'ajax_login_do' ||
                $_REQUEST['c'] == 'member' && $_REQUEST['m'] == 'login_do' ||
                $_REQUEST['c'] == 'page' ||
                $_REQUEST['c'] == 'member' && $_REQUEST['m'] == 'regist'
            ){
                
            }else{
                redirect(site_url('c=member&m=login'));
            }
            */
        }

        //站点地址
        $this->_data['base_url'] = base_url();
        $this->_data['static_url'] = base_url().'site/statics';
        
        //通用用户数据处理
    	$user_data = array();
    	foreach ($_REQUEST as $k=>$v){
    		if(is_string($v)){
	    		$v = trim($v);
    		}
    		$user_data[$k] = $v;
    	}
    	$this->p = $user_data;
    	
    }
    
    function page_url(){
        $p = $this->p;
    	if(empty($p) || !is_array($p)) return site_url();
    	$arr = array();
    	foreach ($p as $k=>$v){
			$arr[] = $k .'='.$v;	    	
    	}
    	return site_url(implode('&', $arr));
    }
    
    
    function check_page_login(){
    	if(!$this->is_login){
    		redirect(site_url('c=member&m=login'));
    		exit;
    	}
    }
    
    function page_to_index(){
  		redirect(site_url());
   		exit;
    }
    
    function check_ajax_login(){
    	if(!$this->is_login){
    		ejd('unlogin', '', site_url('c=member&m=login'));
    	}
    }
    
    function splash($href,$msg){
    	$this->_data['msg'] = $msg;
    	$this->_data['href'] = $href;
    	$this->load->view('splash',$this->_data);
    }
    
    function upload(){
    	require_once LIB_PATH.'thumb/ThumbLib.inc.php';
		$c = $this->config->item('thumb');
		
		$arr_filename = explode('.', $_FILES['Filedata']['name']);
		$extname = $arr_filename[count($arr_filename)-1];
		$new_filename = rand_filename().'.'.$extname; 

		$small_file_path = SPIC_DIR.$new_filename;
		$mdl_file_path = MPIC_DIR.$new_filename;
		$big_file_path = BPIC_DIR.$new_filename;
		$file_path = PIC_DIR.$new_filename;
		
		$upload_small_file_path = 'upload/pic/s/'.$new_filename;
		$upload_mdl_file_path = 'upload/pic/m/'.$new_filename;
		$upload_big_file_path = 'upload/pic/b/'.$new_filename;
		$upload_file_path = 'upload/pic/'.$new_filename;
		
		$data = array();
		$tempFile = $_FILES['Filedata']['tmp_name'];
		
		$thumb = PhpThumbFactory::create($tempFile);
		$thumb->adaptiveResize($c['w'], $c['h']);
		$thumb->save($small_file_path);
		
		$thumb = PhpThumbFactory::create($tempFile);
		$thumb->resize($c['mw']);
		$thumb->save($mdl_file_path);
		
		$thumb = PhpThumbFactory::create($tempFile);
		$thumb->resize($c['bw']);
		$thumb->save($big_file_path);

		$thumb = PhpThumbFactory::create($tempFile);
		$thumb->save($file_path);
		
		$data['small_path'] = $upload_small_file_path;
		$data['mdl_path'] = $upload_mdl_file_path;
		$data['big_path'] = $upload_big_file_path;
		$data['path'] = $upload_file_path;
		
		$this->load->model('img_model');
		$id = $this->img_model->add($data);
		
		echo json_encode(array('id'=>$id,'spath'=>config_item('base_url').$upload_small_file_path));
        

		
    }	
    
    
    function findnone(){
    	$this->load->view('findnone',$this->_data);
    }
    
	function avatar(){
	    
         require_once LIB_PATH.'thumb/ThumbLib.inc.php';
		$c = $this->config->item('avatar');
		
		$arr_filename = explode('.', $_FILES['Filedata']['name']);
		$extname = $arr_filename[count($arr_filename)-1];
		$new_filename = rand_filename().'.'.$extname; 

		$small_file_path = SAVATAR_DIR.$new_filename;
		$mdl_file_path = MAVATAR_DIR.$new_filename;
		$file_path = AVATAR_DIR.$new_filename;
		
		$upload_small_file_path = 'upload/avatar/s/'.$new_filename;
		$upload_mdl_file_path = 'upload/avatar/m/'.$new_filename;
		$upload_file_path = 'upload/avatar/'.$new_filename;
		
		$data = array();
		$tempFile = $_FILES['Filedata']['tmp_name'];
		
		$thumb = PhpThumbFactory::create($tempFile);
		$thumb->adaptiveResize($c['sw'], $c['sh']);
		$thumb->save($small_file_path);
		
		$thumb = PhpThumbFactory::create($tempFile);
		$thumb->adaptiveResize($c['mw'], $c['mh']);
		$thumb->save($mdl_file_path);
		
		$thumb = PhpThumbFactory::create($tempFile);
		$thumb->resize($c['bw']);
		$thumb->save($file_path);

		$data['small_path'] = $upload_small_file_path;
		$data['mdl_path'] = $upload_mdl_file_path;
		$data['path'] = $upload_file_path;
		
		$id = $this->cm->save($this->tbl->avatar,$data);
		
		echo json_encode(array('id'=>$id,'spath'=>config_item('base_url').$upload_small_file_path)); 
		
	}
    
    
    
}
